﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using TestData.Builders;

namespace TestData
{
    public class RandomData
    {

        public const string ResourceName = "SocialFront.Tests.TestData.Data.{0}";
        public static List<string> lastNames = new List<string>();
        public static List<string> firstMale = new List<string>();
        public static List<string> firstFemale = new List<string>();
        public static List<string> usedNames = new List<string>();
        public static List<CSZ>    zipCodes = new List<CSZ>();
        public static List<string> streetParts1 = new List<string>();
        public static List<string> streetParts2 = new List<string>();
        public static List<string> streetParts3 = new List<string>();
        public static List<string> suffix = new List<string>();
        public static List<string> prefixMale = new List<string>();
        public static List<string> prefixFemale = new List<string>();
        public static List<string> comments = new List<string>();
        public static List<string> industry = new List<string>();
        public static List<string> bussiness = new List<string>();
        public static List<string> groceries = new List<string>();
        public static List<string> adjectives = new List<string>();
        public static List<string> ReadFile(string mPath)
        {
            string strFilename = mPath;
            string strLine;
       
            List<string> ret = new List<string>();
            Assembly assembly = Assembly.GetExecutingAssembly();
            Stream stream = assembly.GetManifestResourceStream(
                string.Format(ResourceName, mPath));

            System.IO.StreamReader objStreamReader = new StreamReader (stream);

            while (objStreamReader.Peek() != -1)
            {
                strLine = objStreamReader.ReadLine();
                ret.Add(strLine.Trim());
            }
            objStreamReader.Close();
            return ret;
        }
      
        public static void Init()
        {
            RND.Randomize();

            string path = System.Environment.CurrentDirectory;            
            lastNames = ReadFile("last.txt");
            firstMale = ReadFile("male.txt");
            firstFemale = ReadFile("female.txt");
            comments = ReadFile("comment.txt");
            bussiness = ReadFile("business.txt");
            industry = ReadFile("Industry.txt");
            adjectives = ReadFile("Adjective.txt");
            groceries = ReadFile("grocery.txt");


            List<string> zipTemp = ReadFile("zip5.txt");
            foreach (string s in zipTemp)
            {
                zipCodes.Add(new CSZ(s));
            }

            List<string> streetTemp  = ReadFile("streetparts.txt");
            foreach (string s in streetTemp)
            {
                string[] ary = s.Split(',');
                streetParts1.Add(ary[0]);
                streetParts2.Add(ary[1]);
                streetParts3.Add(ary[2]);
            }
        }

        public static void CheckInit()
        {
            if (lastNames.Count == 0)
                Init();
        }

        public static Gender GetRandomGender()
        {
            int num = RND.GetRandomInt(1, 2);
            return (Gender)Enum.ToObject(typeof(Gender), num);
        }
        public static AgeRange GetRandomAgeRange()
        {
             int num = RND.GetRandomInt(1, 2);
             return (AgeRange)Enum.ToObject(typeof(AgeRange), num);
        }
        public static RandomPerson GetRandomPerson()
        {
            return new PersonBuilder().WithAgeRange(AgeRange.Adult).Build();
        }
        public static RandomAddress GetRandomAddress()
        {
            return AddressBuilder.Build();
        }
        public static string GetGrocery()
        {
            int idx = RND.GetRandomInt(1, groceries.Count - 1);
            return groceries[idx];
        }
        public static string GetAdjective()
        {
            int idx = RND.GetRandomInt(1, adjectives.Count-1);
            return adjectives[idx];
        }
        public static string GetComment()
        {
            int idx = RND.GetRandomInt(1, comments.Count - 1);
            return comments[idx];
        }
    }
    public enum Gender { Male = 1, Female = 2 };
    public enum AgeRange { Child = 1, Adult = 2 };
    public struct CSZ
    {
        public string ZipCode;
        public string City;
        public string State;
        public decimal Latitude;
        public decimal Longitude;
        public string County;

        public CSZ(string s)
        {
            string[] ary = s.Split(',');
            this.ZipCode = ary[0];
            this.City = ary[1];
            this.State = ary[2];
            this.Latitude = decimal.Parse(ary[3]);
            this.Longitude = decimal.Parse(ary[4]);
            this.County = ary[5];
        }

    }
}
